import os
import shutil
from unrar import rarfile

srcPath = "D:/课题/data/wzj/"
targetPath = "D:/课题/data/result/decompress1"
# 正常文件
normal = targetPath + "/normal"
# 失败文件
wrong = targetPath + "/wrong"
# 失败日志
log = targetPath + "/wrong.txt"
# txt文件
txt = targetPath + "/txt"
if __name__ == '__main__':
    if not os.path.exists(normal):
        os.makedirs(normal)  # 创建路径
    if not os.path.exists(wrong):
        os.makedirs(wrong)  # 创建路径
    if not os.path.exists(txt):
        os.makedirs(txt)  # 创建路径

    list = os.listdir(srcPath)
    num = 0

    for i in list:
        srcDir = srcPath + i
        num = num + 1
        print(str(num) + " " + os.path.basename(i))
        name = os.path.splitext(i)[0]
        type = os.path.splitext(i)[1]
        if type != ".rar" and type != ".RAR" and type != ".zip" and type != ".ZIP":
            continue
        try:
            if type == ".rar" or type == ".RAR":
                folder_abs = txt + "/" + name

                rf = rarfile.RarFile(srcDir, mode='r')  # mode的值只能为'r'
                rf_list = rf.namelist()  # 得到压缩包里所有的文件
                print('rar文件内容', rf_list)
                for old_name in rf_list:
                    # 获取文件大小，目的是区分文件夹还是文件，如果是空文件应该不好用。
                    file_size = rf.getinfo(old_name).file_size
                    # # 由于源码遇到中文是cp437方式，所以解码成gbk，windows即可正常
                    # temp_name = old_name.encode('cp437').decode('gbk')
                    new_name = old_name.split("\\")[-1]
                    # 拼接文件的保存路径
                    new_path = folder_abs + "/" + new_name
                    # 判断文件是文件夹还是文件
                    if file_size > 0:
                        # 是文件，通过open创建文件，写入数据
                        if not os.path.exists(folder_abs):
                            os.makedirs(folder_abs)  # 创建路径
                        rf.extract(old_name, folder_abs)  # 循环解压，将文件解压到指定路径

                        # with open(file=new_path, mode='wb') as f:
                        #     # zf.read 是读取压缩包里的文件内容
                        #     f.write(rf.read(old_name))

            elif type == ".zip" or type == ".ZIP":
                continue
                folder_abs = txt + "/" + name
                zip_file = zipfile.ZipFile(srcDir, 'r', zipfile.ZIP_DEFLATED)
                zip_list = zip_file.namelist()  # 得到压缩包里所有文件
                for old_name in zip_list:
                    # 获取文件大小，目的是区分文件夹还是文件，如果是空文件应该不好用。
                    file_size = zip_file.getinfo(old_name).file_size
                    # 由于源码遇到中文是cp437方式，所以解码成gbk，windows即可正常
                    temp_name = old_name.encode('cp437').decode('gbk')
                    new_name = temp_name.split('/')[-1]
                    # 拼接文件的保存路径
                    new_path = folder_abs + "/" + new_name
                    # 判断文件是文件夹还是文件
                    if file_size > 0:
                        # 是文件，通过open创建文件，写入数据
                        if not os.path.exists(folder_abs):
                            os.makedirs(folder_abs)  # 创建路径
                        with open(file=new_path, mode='wb') as f:
                            # zf.read 是读取压缩包里的文件内容
                            f.write(zip_file.read(old_name))

                zip_file.close()  # 关闭文件，必须有，释放内存


        except BaseException as e:
            print(e)
            shutil.copyfile(srcDir, wrong + "/" + i)

            with open(log, "a", encoding="utf-8") as file:  # "a"代表追加内容
                file.write(i + " " + repr(e) + "\n")

        else:
            shutil.copyfile(srcDir, normal + "/" + i)
